/******************************************************************************************
 * Import
 ******************************************************************************************/

// module

import { Base64, Gen, Time, Zip, Session, Storage, Http, Toast, Loading,} from '../../../library/func';

import _ from 'lodash';


/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class DoHttp{

    static loginurl = "/screen/login/index";

    static homeurl = "/screen/home/index";

    //restful请求前缀链接
    static url = "http://192.168.0.125:10000/he_rushbuy/worker/page/";


    //统一的header头部  登录权限原因会做修改
    static header = {
        'content-type': "application/x-www-form-urlencoded;charset=UTF-8",
        'authorization': JSON.stringify({
            token: "empty", 
            arrive: "testarrive",
            platform: "miniprogram",
            appname: "rushbuy",
            version: "v0.0.2"
        })
    }
    

    //默认各种参数各种配置
    static _defaultconf(obj){
        let conf = {

            //config
            config: {
				parse: 'hesq',
                debug: true,
                header: this.header,
                indicator: {
                    start:() => {
                        console.log('http start');
                    },
                    end:() => {
                        console.log('http end');
                    }
                },
                fail: {
                    status:(res, obj) => {
                        console.log('statusError')
                    },
                    parse:(text, obj) => {
                        console.log('parseError')
                    },
                    format:(res, obj) => {
                        obj.error(res);
                        console.log('返回值不为0，在函数中做判断');
                    },
                    network:(err) => {
                        console.log('networkERror')
                    }
                }
            },
            //config


            url: this.url+obj.url,
            data: obj.data ? obj.data:{},
            success: obj.success ? obj.success : (res) => { console.log('正确返回',res)},
            error: obj.error ? obj.error : (res) => { console.log('错误code返回',res)},





        }
        return conf;
    }

    //带有回执后 token 的外层方法
    static _doit(callback){
		Storage.get({
            key:'token',
            success:(res)=>{
                if(res == null){
                    wx.redirectTo({
                        url: this.loginurl
                    })
            
                }else{
                    this.header.authorization = JSON.stringify({
                        token: res, 
                        arrive: "testarrive",
                        platform: "miniprogram",
                        appname: "rushbuy",
                        version: "v0.0.2"
                    });
                    callback();    
                }
            }
        })

    }



    //普通get
    static get(obj){

        this._doit(()=>{
            Http.get( this._defaultconf(obj) );
        })

    }

    //普通post
    static post(obj){

        this._doit(()=>{
            Http.post( this._defaultconf(obj) );
        })
        
    }

    //加载数据
    static Loading(obj, text="加载中"){

        this._doit(()=>{
            Loading.show(text);

            let conf = this._defaultconf(obj);
            conf.success = (data) => {
                Loading.hide();
                obj.success(data);
            }

            Http.get( conf );
        })
    
    }


    //auth登录使用专属
    static Login(openid, userInfo){
        if(!openid || !userInfo){
            console.log("缺少参数");return;
        }
        this.Cpost({
            url:'arrive/login.php',
            data:{
                openid:openid,
                set_head: userInfo.avatarUrl,
                set_nick: userInfo.nickName
            },
            success:(res)=>{
                //储存用户信息  除了 openid
                Storage.set({
                    key: 'userInfo',
                    value: userInfo,
                    success:()=>{
                        console.log('成功储存除了openid以外的用户信息', userInfo);
                    }
                });
                //储存 openid
                Storage.set({
                    key: 'openid',
                    value: openid,
                    success:()=>{
                        console.log('成功储存openid', openid);
                    }
                });
                //储存 openid
                Storage.set({
                    key: 'token',
                    value: res.token,
                    success:()=>{
                        console.log('成功储存token', res.token);
                    }
                });

                wx.redirectTo({
                    url: this.homeurl,
                })


            }
        })

    }


    //纯净版post 用于登录或者其他特殊用途页面
    static Cpost(obj){
        Http.post( this._defaultconf(obj) );
    }




}


